﻿using Microsoft.AspNetCore.Mvc;
using RobosimAPI.Class;

namespace RobosimAPI.Controllers
{
	[Route("back/Api2/Client/checkRemainSubmitTimes")]
	public class CheckRemainSubmitTimesController : ControllerBase
	{
		[HttpPost]
		public IActionResult CheckRemainSubmit([FromForm] CheckRemainSubmitTimesModel model)
		{
			Account account = new Account(model.user_id);
			Round round = new Round(model.round_id);
			Group group = new Group(round.GroupId);
			Event @event = new Event(group.event_id);
			int[] join = account.GetAllJoinedEvents();
			if (Array.IndexOf(join, group.id) == -1)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "Has not apply" });
			}
			if (round.IsLimit != 1)
			{
				return Ok(new CheckRemainSubmitTimesResponse() { status = 1, data = "unlimited" });
			}
			Score score = new Score(@event.db_name, account.user_id, round.Id);
			if (score.user_id<=0)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "Has not apply" });
			}
			return Ok(new CheckRemainSubmitTimesResponse() { status = 1, data = score.sub_limit.ToString() });
		}
		public class CheckRemainSubmitTimesModel
		{
			public int user_id { get; set; }
			public int group_id { get; set; }
			public int map_id { get; set; }
			public int round_id { get; set; }
		}
		public class CheckRemainSubmitTimesResponse
		{
			public int status { get; set; }
			public string data { get; set; }
		}
	}
}
